"""
src/ode3d/wxapp.py
    A top level app class for using wxPython with this library.

Written By:
    James Thomas
    Email: jim@houseoftechnology.org
    Web: http://mission-cognition.houseoftechnology.org/

Copyright 2009-2012

This file is part of the PandaODElib distribution.

This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public License
as published by the Free Software Foundation; either version 3 of
the License, or (at your option) any later version. The text of the
GNU Lesser General Public License is included with this library in
the file named LICENSE.

This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
"""
import itertools

import wx

import ode3d.lib
import pvis.pwx

class TextField(object):
    def __init__(self, Control):
        self.ctrl = Control
        self.visible = True
    
    def setText(self, Text):
        try:
            self.ctrl.SetValue(Text)
        except wx._core.PyDeadObjectError:
            pass
    
    SetValue = setText
    
    @property
    def text(self):
        try:
            return self.ctrl.GetValue()
        except wx._core.PyDeadObjectError:
            return
        
    @text.setter
    def text(self, Value):
        self.setText(Value)
        
class StatusArea(object):
    def __init__(self, numRows, numCols, fontSize, *args, **kwargs):
        self.panel = wx.Panel(*args, **kwargs)
        
        gs = wx.GridSizer(numRows, numCols, 5, 5)
        
        font = wx.Font(fontSize, wx.FONTFAMILY_TELETYPE, wx.NORMAL, wx.NORMAL, False)
        self.textBoxes = []
        
        for r, c in itertools.product(xrange(numRows), xrange(numCols)):
            text = wx.TextCtrl(self.panel, wx.ID_ANY, style=wx.ALIGN_TOP | wx.ALIGN_LEFT | wx.TE_MULTILINE | wx.HSCROLL)
            text.SetFont(font) 
            text.SetValue('Box %02d (%d,%d):\nThis is text\nAnd another line' % (len(self.textBoxes), c, r))
            self.textBoxes.append(TextField(text))
            gs.Add(text, 0, wx.EXPAND)

        self.panel.SetAutoLayout(True)
        self.panel.SetSizer(gs)

class wxOdeApp(pvis.pwx.PandaApp, ode3d.lib.OdeApp):
    def __init__(self, *args, **kwargs):
        pvis.pwx.PandaApp.__init__(self)
        ode3d.lib.OdeApp.__init__(self, *args, **kwargs)

    def UpdateSettings(self):
        # Give wxPython a chance to handle events before we do a sim step
        # if the sim is running slow the timing loop prevents UI from
        # working well.  This seems to fix it.
        app = wx.GetApp()
        while app.Pending(): 
            app.Dispatch()
            
        # Now we can go ahead and do the updates
        ode3d.lib.OdeApp.UpdateSettings(self)

    def UpdateDisplay(self):
        # Give wxPython a chance to handle events before we display
        # if the sim is running slow the timing loop prevents UI from
        # working well.  This seems to fix it.
        app = wx.GetApp()
        while app.Pending(): 
            app.Dispatch()
            
        # Now we can go ahead and do the updates
        ode3d.lib.OdeApp.UpdateDisplay(self)
